package com.example.litemall.admin.api.service.impl;

import java.util.*;

import com.example.litemall.admin.api.commons.MapperUtils;
import com.example.litemall.admin.api.domain.LitemallRole;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.example.litemall.admin.api.mapper.LitemallRoleMapper;
import com.example.litemall.admin.api.service.LitemallRoleService;
import tk.mybatis.mapper.entity.Example;

@Service
public class LitemallRoleServiceImpl implements LitemallRoleService {

    @Resource
    private LitemallRoleMapper litemallRoleMapper;

    @SneakyThrows
    @Override
    public Set<String> getRolesByIds(String rids) {
        List<Integer> ridList = MapperUtils.json2list(rids, Integer.class);

        Set<String> roles = new HashSet<String>();
        if (rids.isEmpty()) {
            return roles;
        }
        Example example = new Example(LitemallRole.class);

        example.or().andIn("id", ridList).andEqualTo("enabled", true)
                .andEqualTo("deleted", false);

        List<LitemallRole> litemallRoles = litemallRoleMapper.selectByExample(example);
        for (LitemallRole litemallRole : litemallRoles) {
            roles.add(litemallRole.getName());
        }

        return roles;
    }
}
